/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * 
 * @author yaoyuming
 * 
 * @date 2020-05-06 11:43
 */
package com.yao.fin.baidu;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yaoyuming
 * @date 2020/05/06 11:43
 */
@Service
@Slf4j
public class TokenServiceImpl implements TokenService {

    public static void main(String[] args) {
        String accessToken = new TokenServiceImpl().getAccessToken(BaiduApiConst.API_KEY, BaiduApiConst.SECRET_KEY);

        System.out.println("accessToken = " + accessToken);
    }

    @Override
    public String getAccessToken(String apiKey, String securetKey) {

        String token = "";

        String getAccessTokenUrl = String.format(BaiduApiConst.ACCESS_TOKEN_URL, apiKey, securetKey);

        String result = HttpUtil.post(getAccessTokenUrl, "");

        log.info("请求百度获取token链接:{} ;;; 结果:{}", getAccessTokenUrl, result);

        if (StrUtil.isNotEmpty(result)) {
            token = JSON.parseObject(result).getString("access_token");
        }

        return token;
    }


}